package com.dz.lm.service.comp.label.impl;

import com.dz.lm.common.dto.AttachDTO;
import com.dz.lm.common.dto.CompLabelDTO;
import com.dz.lm.common.dto.PageDto;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.dao.complabel.read.CompLabelDao;
import com.dz.lm.domain.complabel.CompLabel;
import com.dz.lm.domain.complabel.CompLabelExample;
import com.dz.lm.service.attach.AttachService;
import com.dz.lm.service.comp.label.CompLabelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2020/6/6 13:54;
 */
@Service
public class CompLabelServiceImpl implements CompLabelService {
    @Resource(name = "readCompLabelDao")
    private CompLabelDao readCompLabelDao;
    @Resource(name = "writeCompLabelDao")
    private com.dz.lm.dao.complabel.write.CompLabelDao writeCompLabelDao;
    @Autowired
    private AttachService attachService;
    @Override
    public int add(CompLabel compLabel) throws ServiceException {

        return writeCompLabelDao.insertSelective(compLabel);
    }

    @Override
    public int delete(int labelId) throws ServiceException {
        return writeCompLabelDao.deleteByPrimaryKey(labelId);
    }

    @Override
    public int update(CompLabel compLabel) throws ServiceException {

        return writeCompLabelDao.updateByPrimaryKeySelective(compLabel);
    }

    @Override
    public PageDto query(int pageNum , int rwos ,int compId) throws ServiceException {
        PageDto pageDto = new PageDto();
        PageHelper.startPage(pageNum,rwos);
        CompLabelExample ex = new CompLabelExample();
        CompLabelExample.Criteria cr = ex.createCriteria();
        cr.andCompIdEqualTo(compId);
        List<CompLabel> list = readCompLabelDao.selectByExample(ex);
        if(list != null && list.size() > 0){
            List<CompLabelDTO> dtos = new ArrayList();
            for (CompLabel label : list) {
                CompLabelDTO dto = new CompLabelDTO();
                BeanUtils.copyProperties(label,dto);
                setExt(dto);
                dtos.add(dto);
            }
            PageInfo pageInfo = new PageInfo(list);
            pageDto.setInfo(pageInfo);
            pageDto.setList(dtos);
        }
        return pageDto;
    }

    @Override
    public CompLabelDTO get(int labelId) throws ServiceException {
        CompLabelDTO dto = null;
        CompLabel compLabel = readCompLabelDao.selectByPrimaryKey(labelId);
        if(compLabel != null){
            dto = new CompLabelDTO();
            BeanUtils.copyProperties(compLabel,dto);
            setExt(dto);
        }
        return dto;
    }

    /**
     * 补充信息
     * @param dto
     */
    private void  setExt(CompLabelDTO dto ){
        if(dto.getVideo() > 0){
            try {
                AttachDTO attach = attachService.sleletById(dto.getVideo());
                dto.setAttach(attach);
            } catch (ServiceException e) {
                e.printStackTrace();
            }
        }
    }
}
